'use strict'

/**
 * @description 汇总所有API接口
 */
const APILIST = (function ($, global) {
  const layer = global.layer

  const request = function (options) {
    return $.ajax({
      type: 'POST',
      dataType: 'json',
      ...options,
    })
  }

  return {
    parentConf: null,
    userConf: null,

    // 获取lineId
    getLineId() {
      const params = new URL(location.href).searchParams
      return params.get('line_id')
    },

    // 获取用可用的 `use_extends` 字段
    getBaseInfo: function (callback) {
      const that = this
      const URL = '/app/admin/v1-api/getUserExtend?type=2'
      $.ajax({
        url: URL,
        type: 'get',
        success: function (res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          const { parent_user_extend, user_extend } = res.data
          that.parentConf = parent_user_extend
          that.userConf = user_extend
          callback && callback(user_extend)
        },
      })
    },

    // 续费时间列表
    getNodeRenewDurationList(lineId, callback) {
      request({
        url: `/app/admin/v1-api/getNodeRenewDurationList?line_id=${lineId}`,
        data: {
          line_id: lineId,
        },
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res.data)
        },
      })
    },

    // 执行续费 - 执行续费时间
    postRenewDuration(data, callback) {
      request({
        url: '/app/admin/v1-api/renew',
        data: data,
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res)
        },
      })
    },

    // 搜索下级会员列表
    getInvitationUserListURL: '/app/admin/v1-api/getInvitationUserList',
    // getInvitationUserList(userName, callback) {
    //   request({
    //     url: '/app/admin/v1-api/getInvitationUserList',
    //     data: {
    //       username: userName,
    //     },
    //     success(res) {
    //       if (res.code) return global.layer.msg(res.msg)
    //       callback && callback(res.data)
    //     },
    //   })
    // },

    // 过户
    setTransferUser(data, callback) {
      request({
        // line_id 线路ID
        // user_id 会员ID
        // ids 订单节点ID 例如[4,3,2,1]
        url: '/app/admin/v1-api/transfer',
        data,
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res)
        },
      })
    },

    // 获取平均时间列表(文档)
    getAverageTimeList(callback) {
      request({
        url: '/app/admin/v1-api/getAverageTimeList',
        data: {},
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res.data)
        },
      })
    },

    // 平均时间
    setAverageTime(data, callback) {
      request({
        // line_id 线路ID
        // ids 订单节点ID 例如[4,3,2,1]
        url: '/app/admin/v1-api/averageTime',
        data,
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res)
        },
      })
    },

    // 修改密码
    putUserUpdatePwd(data, callback) {
      request({
        // type=&data=[{"id":1,"password":""},{"id":2,"password":""}]&password=
        // type 类型1=批量修改,2=列表修改
        // data
        // password 密码type=1必传
        url: '/app/admin/v1-api/updatePwd',
        data,
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res)
        },
      })
    },

    // 调换-自定义账号列表
    getCustomAccountList(callback) {
      //
      request({
        url: '/app/admin/v1-api/getCustomTypeListExchange',
        data: {},
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res.data)
        },
      })
    },

    // 调换
    putExchange(data, callback) {
      /**
        {
          "type": 1, 
          "line_id": 1, 
          "custom_type": 0, 
          "account": "", 
          "pass": "", 
          "ids": 3, 
          "data": [
              {
              "region_id": 1, 
              "number": 1 
              }
            ] 
        }*/
      request({
        url: '/app/admin/v1-api/exchange',
        data,
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res)
        },
      })
    },
    // http://127.0.0.1:8787
    // 获取线路列表
    getLines(data, callback) {
      request({
        url: '/app/admin/v1-api/getLineIds',
        data: {
          line_id: defaultLineId,
          ...data,
        },
        success(res) {
          if (res.code) {
            layer.closeAll('loading')
            return layer.msg(res.msg)
          }
          callback && callback(res.data)
        },
      })
    },
  }
})(layui.$, layui)
